import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

public class Download extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ServletConfig config;

	private String filename;
	
	/**
	 * Init the servlet
	 */

	final public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}

	final public ServletConfig getServletConfig() {
		return config;
	}

	/**
	 * Handles GET requests
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		filename = request.getParameter("filename");

		SmartUpload mySmartUpload = new SmartUpload();

		try {
			mySmartUpload.initialize(config, request, response);
			mySmartUpload.setContentDisposition(null);
			mySmartUpload.downloadFile("/downloads/" + filename, null,new String(filename.getBytes("GBK"), "ISO-8859-1"));
		} catch (Exception e) {
			// Log.err(this.getClass(), "doPost", e);
			try {
				request.setAttribute("msg", "����ʧ��");
				request.getRequestDispatcher("../index/error.jsp").forward(
						request, response);
			} catch (Exception ex) {
			}
		}
	}

	/**
	 * Handles POST requests
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * Destroy the servlet
	 */
	public void destroy() {

	}
}
